<?php
class Mysql{
    private $_host;
    private $_user;
    private $_pwd;
    private $_db;
    private $_char;
    private $_link;
    public $config;
    private static $once=null;
//单例模式
    private function __construct($config){
        $this->init($config);
        $this->connect();
        $this->charSet();
        $this->selectDb();
    }

    private function __clone(){

    }

    public static function  GetOnce($config){
        if(null===static::$once){
            static::$once = new Mysql($config);
        }
        return static::$once;
    }

    private function init($config){
        $this->_host=isset($config['host'])?$config['host']:'localhost';
        $this->_user=isset($config['user'])?$config['user']:'root';
        $this->_pwd=isset($config['pwd'])?$config['pwd']:'root';
        $this->_db=isset($config['db'])?$config['db']:'test2';
        $this->_char=isset($config['char'])?$config['char']:'utf8';
    }

    private function connect(){
        @$this->_link=mysql_connect($this->_host,$this->_user,$this->_pwd);
        if(!$this->_link){
            die('连接失败,错误信息:'.mysql_error());
        }
    }

    private function charSet(){
        mysql_set_charset($this->_char);
    }

    private function selectDb(){
        mysql_select_db($this->_db);
    }
//查询方法
    public function dql($sql){
        $res = mysql_query($sql);
        if(!$res){
            die('查询失败！错误信息：'.mysql_error());
        }
        $arr = array();
        while($row = mysql_fetch_assoc($res)){
            $arr[] = $row;
        }
        mysql_free_result($res);
        return $arr;
    }
//增删改方法
    public function dml($sql){
        $res = mysql_query($sql);
        if(!$res){
            die('编辑失败！错误信息：'.mysql_error());
        }
        return mysql_affected_rows();
    }
}
?>